package v1

import (
	"context"

	v1pb "quipus/proto/gen/api/v1"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

func (s *APIV1Service) GetSystemProfile(ctx context.Context, _ *v1pb.GetSystemProfileRequest) (*v1pb.SystemProfile, error) {
	workspaceProfile := &v1pb.SystemProfile{
		Version:      s.Profile.Version,
		Mode:         s.Profile.Mode,
		Public:       s.Profile.Public,
		PasswordAuth: s.Profile.PasswordAuth,
		InstanceUrl:  s.Profile.InstanceURL,
	}
	owner, err := s.GetInstanceOwner(ctx)
	if err != nil {
		return nil, status.Errorf(codes.Internal, "failed to get instance owner: %v", err)
	}
	if owner != nil {
		workspaceProfile.Owner = owner.Urn
	} else {
		// If owner is not found, set Public/PasswordAuth to true.
		workspaceProfile.Public = true
		workspaceProfile.PasswordAuth = true
	}
	return workspaceProfile, nil
}

var ownerCache *v1pb.User

func (s *APIV1Service) GetInstanceOwner(ctx context.Context) (*v1pb.User, error) {
	if ownerCache != nil {
		return ownerCache, nil
	}

	// hostUserType := store.RoleHost
	// user, err := s.GetUser(ctx, &store.FindUser{
	// 	Role: &hostUserType,
	// })
	// if err != nil {
	// 	return nil, errors.Wrapf(err, "failed to find owner")
	// }
	// if user == nil {
	// 	return nil, nil
	// }

	// ownerCache = convertUserFromStore(user)
	// return ownerCache, nil
	return nil, nil
}
